package com.sinodata.ns.track;

import com.sinodata.ns.sv.ISupervisor;
import com.sinodata.ns.sv.SupervisorManager;

import jt.classic.system.ISystem;
import jt.classic.system.database.SystemDataSource;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import org.limp.basework.SimpleBean4Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;

import org.limp.mine.DateTrimmer;
import org.limp.mine.XMLTool;

import java.sql.Connection;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 轨迹跟踪
 *
 * @author mido
 *
 */
public class TrackAction extends DispatchAction {
    /**
     * 记录轨迹
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward log(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        Map params = cbh.getResource();

        String userid = (String) params.get("userid");

        Connection conn = null;

        ISupervisor sv = null;

        Map resultMap = new HashMap();
        resultMap.put("opera", "saveTrack");

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            SupervisorManager sm = new SupervisorManager(conn);

            sv = sm.getSupervisorByUserId(userid);
        } catch (Exception ex) {
            resultMap.put("status", "-1");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        if (sv != null) {
            try {
                cbh.getResource().put("USERID", sv.iuser().id());
                cbh.getResource().put("USERNAME", sv.iuser().iname());
                cbh.getResource().put("GROUPID", sv.iuser().igroup().id());
                cbh.getResource().put("GROUPNAME", sv.iuser().igroup().iname());
                cbh.getResource().put("CTIME", DateTrimmer.getYMDHMS());
                cbh.getResource().put("LON", params.get("lon"));
                cbh.getResource().put("LAT", params.get("lat"));
                cbh.getResource().put("BD",
                    (sv.ibd() != null) ? sv.ibd().id() : "0");
                cbh.getResource().put("BDNAME",
                    (sv.ibd() != null) ? sv.ibd().iname() : "");
                cbh.getResource().put("GC",
                    (sv.ibd() != null) ? sv.ibd().ibdname() : "");

                SystemDataSource ds = SystemDataSource.getInstance();
                conn = ds.getConnection();

                BaseworkUtil util = new BaseworkUtil();

                SimpleBean4Table bean = new SimpleBean4TableImpl("APP_NS_TRACK",
                        cbh);

                Map result = (Map) util.manualSave1(bean, conn);

                conn.commit();

                resultMap.put("status", "0");
            } catch (Exception ex) {
                resultMap.put("status", "-1");
                conn.rollback();
                ISystem.catchException(request, this, ex);
            } finally {
                SystemDataSource.closeConnection(conn);
            }
        } else {
            resultMap.put("status", "-1");
        }

        String result = XMLTool.result2XML(resultMap);

        request.setAttribute("result", result);

        return mapping.findForward("result");
    }
}
